<template class="login_template">
    <div class="background" :style="{backgroundImage:'url('+require('../assets/pic3.jpg')+')'}">
      <el-form :rules="rules" class="login-container" label-position="left"
             ref="form" label-width="0px" v-loading="loading" justify="center">
        <h3 class="login_title">网上书城登录</h3>
        <el-form-item prop="account">
          <el-input type="text" v-model="loginForm.username" auto-complete="off" placeholder="账号"></el-input>
        </el-form-item>
        <el-form-item prop="checkPass">
          <el-input type="password" v-model="loginForm.password" auto-complete="off" placeholder="密码"></el-input>
        </el-form-item>
        <el-checkbox class="login_remember" v-model="checked" label-position="left">记住密码</el-checkbox>
        <el-form-item style="width: 100%">
          <el-button type="primary" @click.native.prevent="submitClick" style="width: 100%">登录</el-button>
        </el-form-item>
          <router-link to="/register">没有账号？点击注册</router-link>
      </el-form>
    </div>
</template>
<script>
  import {postRequest} from '../utils/api'
  import {putRequest} from '../utils/api'
  import {myPostRequest} from '../utils/api'
  import {getAllCookies} from '../utils/cookies'
  import {getCookieByKey} from '../utils/cookies'
  import {mySetCookie} from '../utils/cookies'

  
  export default{
    mounted() {
        let userName = this.$route.query.userName;
        if (userName == null | userName == undefined) {
            return;
        } else {
            this.loginForm.username = userName;
        }
    },
    data(){
      return {
        rules: {
          account: [{required: true, message: '请输入用户名', trigger: 'blur'}],
          checkPass: [{required: true, message: '请输入密码', trigger: 'blur'}]
        },
        checked: true,
        loginForm: {
          username: 'zhangpeng',
          password: '123456'
        },
        loading: false,
      }
    },
    methods: {
      submitClick: function () {
        var _this = this;
        this.loading = true;
        myPostRequest('user/login', {
          userName: this.loginForm.username,
          password: this.loginForm.password
        }).then(resp=> {
          _this.loading = false;
          if (resp.data.code == 0) {
              let user = resp.data.data;
              if(user == null || user == undefined) {
                  _this.$message.error('密码错误！');
                  return;
              }
              _this.$store.dispatch('login', user);
              _this.$router.replace({path: '/book'});
              mySetCookie('userId',user.id,3600);
              mySetCookie('userName',user.userName,3600);
              console.log(getCookieByKey('user'));
              console.log(getCookieByKey('userName'));
              _this.$message({
                  message: '恭喜你登录成功！',
                  type: 'success'
              });
            }else {
              _this.$alert('登录失败!', '失败!');
            }
        }, resp=> {
          _this.loading = false;
          _this.$alert('找不到服务器⊙﹏⊙∥!', '失败!还是让你进去吧');
        //   _this.$router.replace({path: '/home'});
        });
      },
      // 获取User Obj
      getUserObj(){
      }
    }
  }
</script>
<style scoped>
  .login-container {
    border-radius: 15px;
    background-clip: padding-box;
    margin:  auto auto;
    width: 350px;
    padding: 35px 35px 15px 35px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
    height: 50vh;
    float: center;
    filter:alpha(opacity);
    opacity:0.7;
    position: absolute;
    left: 500px;
    top:100px;
  }

  .login_title {
    margin: 0px auto 40px auto;
    text-align: center;
    color: #505458;
  }

  .login_remember {
    margin: 0px 0px 35px 0px;
    text-align: left;
  }

  .background{
    width: 100%;
    height: 100%;
    position: absolute;
    background-repeat: no-repeat;
    background-position: center center;
    background-size: cover;
    top: 0px;
    left: 0px;
    /* padding-top: 100hv; */
  }

</style>
